import 'package:flutter/material.dart';
import '../../config/theme/theme_manager.dart';

/// 通用列表项组件
/// 提供统一的列表项样式和布局
class CommonListItem extends StatelessWidget {
  /// 标题
  final String? title;

  /// 副标题
  final String? subtitle;

  /// 描述文本
  final String? description;

  /// 前置图标
  final IconData? leadingIcon;

  /// 前置图片
  final String? leadingImage;

  /// 前置组件
  final Widget? leading;

  /// 后置图标
  final IconData? trailingIcon;

  /// 后置文本
  final String? trailingText;

  /// 后置组件
  final Widget? trailing;

  /// 点击回调
  final VoidCallback? onTap;

  /// 长按回调
  final VoidCallback? onLongPress;

  /// 是否显示分割线
  final bool showDivider;

  /// 是否启用
  final bool enabled;

  /// 是否选中
  final bool selected;

  /// 内边距
  final EdgeInsetsGeometry? padding;

  /// 外边距
  final EdgeInsetsGeometry? margin;

  /// 背景颜色
  final Color? backgroundColor;

  /// 圆角半径
  final double? borderRadius;

  /// 边框
  final Border? border;

  /// 阴影
  final List<BoxShadow>? boxShadow;

  /// 标题样式
  final TextStyle? titleStyle;

  /// 副标题样式
  final TextStyle? subtitleStyle;

  /// 描述样式
  final TextStyle? descriptionStyle;

  /// 后置文本样式
  final TextStyle? trailingTextStyle;

  /// 前置图标颜色
  final Color? leadingIconColor;

  /// 后置图标颜色
  final Color? trailingIconColor;

  /// 前置图标大小
  final double? leadingIconSize;

  /// 后置图标大小
  final double? trailingIconSize;

  /// 前置图片大小
  final double? leadingImageSize;

  /// 垂直间距
  final double spacing;

  /// 水平间距
  final double horizontalSpacing;

  /// 列表项类型
  final ListItemType type;

  const CommonListItem({
    super.key,
    this.title,
    this.subtitle,
    this.description,
    this.leadingIcon,
    this.leadingImage,
    this.leading,
    this.trailingIcon,
    this.trailingText,
    this.trailing,
    this.onTap,
    this.onLongPress,
    this.showDivider = false,
    this.enabled = true,
    this.selected = false,
    this.padding,
    this.margin,
    this.backgroundColor,
    this.borderRadius,
    this.border,
    this.boxShadow,
    this.titleStyle,
    this.subtitleStyle,
    this.descriptionStyle,
    this.trailingTextStyle,
    this.leadingIconColor,
    this.trailingIconColor,
    this.leadingIconSize,
    this.trailingIconSize,
    this.leadingImageSize,
    this.spacing = 4,
    this.horizontalSpacing = 12,
    this.type = ListItemType.normal,
  });

  @override
  Widget build(BuildContext context) {
    final themeManager = ThemeManager.instance;

    Widget listItem = Container(
      margin: margin,
      decoration: BoxDecoration(
        color: _getBackgroundColor(themeManager),
        borderRadius: BorderRadius.circular(borderRadius ?? 0),
        border: border,
        boxShadow: boxShadow,
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: enabled ? onTap : null,
          onLongPress: enabled ? onLongPress : null,
          borderRadius: BorderRadius.circular(borderRadius ?? 0),
          child: Container(
            padding: padding ?? _getDefaultPadding(),
            child: Row(
              children: [
                // 前置组件
                if (_hasLeading()) ...[
                  _buildLeading(themeManager),
                  SizedBox(width: horizontalSpacing),
                ],

                // 主要内容
                Expanded(
                  child: _buildContent(themeManager),
                ),

                // 后置组件
                if (_hasTrailing()) ...[
                  SizedBox(width: horizontalSpacing),
                  _buildTrailing(themeManager),
                ],
              ],
            ),
          ),
        ),
      ),
    );

    if (showDivider) {
      listItem = Column(
        children: [
          listItem,
          Divider(
            height: 1,
            thickness: 1,
            color: themeManager.dividerColor,
            indent: _hasLeading()
                ? (leadingImageSize ?? leadingIconSize ?? 40) +
                    horizontalSpacing +
                    (padding?.horizontal ?? 32)
                : 0,
          ),
        ],
      );
    }

    return listItem;
  }

  /// 是否有前置组件
  bool _hasLeading() {
    return leading != null || leadingIcon != null || leadingImage != null;
  }

  /// 是否有后置组件
  bool _hasTrailing() {
    return trailing != null || trailingIcon != null || trailingText != null;
  }

  /// 获取背景颜色
  Color _getBackgroundColor(ThemeManager themeManager) {
    if (backgroundColor != null) return backgroundColor!;
    if (selected) return themeManager.primaryColor.withOpacity(0.1);
    if (!enabled) return themeManager.backgroundColor.withOpacity(0.5);
    return Colors.transparent;
  }

  /// 获取默认内边距
  EdgeInsetsGeometry _getDefaultPadding() {
    switch (type) {
      case ListItemType.compact:
        return const EdgeInsets.symmetric(horizontal: 12, vertical: 8);
      case ListItemType.normal:
        return const EdgeInsets.symmetric(horizontal: 16, vertical: 12);
      case ListItemType.comfortable:
        return const EdgeInsets.symmetric(horizontal: 20, vertical: 16);
    }
  }

  /// 构建前置组件
  Widget _buildLeading(ThemeManager themeManager) {
    if (leading != null) return leading!;

    if (leadingImage != null) {
      return ClipRRect(
        borderRadius: BorderRadius.circular(4),
        child: Image.asset(
          leadingImage!,
          width: leadingImageSize ?? 40,
          height: leadingImageSize ?? 40,
          fit: BoxFit.cover,
        ),
      );
    }

    if (leadingIcon != null) {
      return Icon(
        leadingIcon,
        size: leadingIconSize ?? 24,
        color: leadingIconColor ??
            (enabled
                ? themeManager.contrastColor.withOpacity(0.7)
                : themeManager.contrastColor.withOpacity(0.3)),
      );
    }

    return const SizedBox.shrink();
  }

  /// 构建主要内容
  Widget _buildContent(ThemeManager themeManager) {
    final List<Widget> children = [];

    // 标题
    if (title != null) {
      children.add(
        Text(
          title!,
          style: titleStyle ??
              TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w500,
                color: enabled
                    ? (selected
                        ? themeManager.primaryColor
                        : themeManager.contrastColor)
                    : themeManager.contrastColor.withOpacity(0.5),
              ),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
      );
    }

    // 副标题
    if (subtitle != null) {
      if (children.isNotEmpty) children.add(SizedBox(height: spacing));
      children.add(
        Text(
          subtitle!,
          style: subtitleStyle ??
              TextStyle(
                fontSize: 14,
                color: enabled
                    ? themeManager.contrastColor.withOpacity(0.7)
                    : themeManager.contrastColor.withOpacity(0.3),
              ),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
      );
    }

    // 描述
    if (description != null) {
      if (children.isNotEmpty) children.add(SizedBox(height: spacing));
      children.add(
        Text(
          description!,
          style: descriptionStyle ??
              TextStyle(
                fontSize: 12,
                color: enabled
                    ? themeManager.contrastColor.withOpacity(0.5)
                    : themeManager.contrastColor.withOpacity(0.3),
                height: 1.4,
              ),
          maxLines: 2,
          overflow: TextOverflow.ellipsis,
        ),
      );
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: children,
    );
  }

  /// 构建后置组件
  Widget _buildTrailing(ThemeManager themeManager) {
    if (trailing != null) return trailing!;

    final List<Widget> children = [];

    // 后置文本
    if (trailingText != null) {
      children.add(
        Text(
          trailingText!,
          style: trailingTextStyle ??
              TextStyle(
                fontSize: 14,
                color: enabled
                    ? themeManager.contrastColor.withOpacity(0.6)
                    : themeManager.contrastColor.withOpacity(0.3),
              ),
          textAlign: TextAlign.right,
        ),
      );
    }

    // 后置图标
    if (trailingIcon != null) {
      if (children.isNotEmpty) children.add(const SizedBox(width: 8));
      children.add(
        Icon(
          trailingIcon,
          size: trailingIconSize ?? 20,
          color: trailingIconColor ??
              (enabled
                  ? themeManager.contrastColor.withOpacity(0.5)
                  : themeManager.contrastColor.withOpacity(0.3)),
        ),
      );
    }

    return Row(
      mainAxisSize: MainAxisSize.min,
      children: children,
    );
  }
}

/// 列表项类型
enum ListItemType {
  compact,
  normal,
  comfortable,
}

/// 设置列表项
class SettingListItem extends StatelessWidget {
  final String title;
  final String? subtitle;
  final IconData? icon;
  final Widget? trailing;
  final VoidCallback? onTap;
  final bool showArrow;
  final Color? iconColor;

  const SettingListItem({
    super.key,
    required this.title,
    this.subtitle,
    this.icon,
    this.trailing,
    this.onTap,
    this.showArrow = true,
    this.iconColor,
  });

  @override
  Widget build(BuildContext context) {
    return CommonListItem(
      title: title,
      subtitle: subtitle,
      leadingIcon: icon,
      leadingIconColor: iconColor,
      trailing: trailing,
      trailingIcon: showArrow ? Icons.chevron_right : null,
      onTap: onTap,
      type: ListItemType.normal,
    );
  }
}

/// 用户列表项
class UserListItem extends StatelessWidget {
  final String name;
  final String? subtitle;
  final String? avatar;
  final Widget? trailing;
  final VoidCallback? onTap;
  final bool online;

  const UserListItem({
    super.key,
    required this.name,
    this.subtitle,
    this.avatar,
    this.trailing,
    this.onTap,
    this.online = false,
  });

  @override
  Widget build(BuildContext context) {
    final themeManager = ThemeManager.instance;

    return CommonListItem(
      title: name,
      subtitle: subtitle,
      leading: Stack(
        children: [
          CircleAvatar(
            radius: 20,
            backgroundColor: themeManager.primaryColor.withOpacity(0.1),
            backgroundImage: avatar != null ? AssetImage(avatar!) : null,
            child: avatar == null
                ? Icon(
                    Icons.person,
                    color: themeManager.primaryColor,
                  )
                : null,
          ),
          if (online)
            Positioned(
              right: 0,
              bottom: 0,
              child: Container(
                width: 12,
                height: 12,
                decoration: BoxDecoration(
                  color: Colors.green,
                  shape: BoxShape.circle,
                  border: Border.all(
                    color: themeManager.backgroundColor,
                    width: 2,
                  ),
                ),
              ),
            ),
        ],
      ),
      trailing: trailing,
      onTap: onTap,
      type: ListItemType.normal,
    );
  }
}

/// 通知列表项
class NotificationListItem extends StatelessWidget {
  final String title;
  final String? content;
  final String? time;
  final bool isRead;
  final IconData? icon;
  final Color? iconColor;
  final VoidCallback? onTap;

  const NotificationListItem({
    super.key,
    required this.title,
    this.content,
    this.time,
    this.isRead = false,
    this.icon,
    this.iconColor,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    final themeManager = ThemeManager.instance;

    return CommonListItem(
      title: title,
      subtitle: content,
      description: time,
      leadingIcon: icon ?? Icons.notifications,
      leadingIconColor: iconColor ?? themeManager.primaryColor,
      trailing: !isRead
          ? Container(
              width: 8,
              height: 8,
              decoration: BoxDecoration(
                color: themeManager.primaryColor,
                shape: BoxShape.circle,
              ),
            )
          : null,
      onTap: onTap,
      backgroundColor:
          !isRead ? themeManager.primaryColor.withOpacity(0.05) : null,
      type: ListItemType.normal,
    );
  }
}

/// 菜单列表项
class MenuListItem extends StatelessWidget {
  final String title;
  final String? subtitle;
  final IconData icon;
  final Color? iconColor;
  final Color? backgroundColor;
  final VoidCallback? onTap;
  final Widget? trailing;
  final bool showArrow;

  const MenuListItem({
    super.key,
    required this.title,
    this.subtitle,
    required this.icon,
    this.iconColor,
    this.backgroundColor,
    this.onTap,
    this.trailing,
    this.showArrow = true,
  });

  @override
  Widget build(BuildContext context) {
    final themeManager = ThemeManager.instance;

    return CommonListItem(
      title: title,
      subtitle: subtitle,
      leading: Container(
        width: 36,
        height: 36,
        decoration: BoxDecoration(
          color: backgroundColor ?? themeManager.primaryColor.withOpacity(0.1),
          borderRadius: BorderRadius.circular(8),
        ),
        child: Icon(
          icon,
          size: 20,
          color: iconColor ?? themeManager.primaryColor,
        ),
      ),
      trailing: trailing,
      trailingIcon: showArrow ? Icons.chevron_right : null,
      onTap: onTap,
      type: ListItemType.normal,
    );
  }
}
